/*
 * Copyright (c) 2017 22420103@QQ.COM 版权所有
 */
package com.lehe.controller;

import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.WriterException;
import com.google.zxing.client.j2se.MatrixToImageConfig;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import com.lehe.common.MessageEnumDto;
import com.lehe.dao.GoldDetailDao;
import com.lehe.dao.PayRecordDao;
import com.lehe.dao.XHUserListDao;
import com.lehe.entry.UserGoldDetailDto;
import com.lehe.entry.xh.ApplyDto;
import com.lehe.entry.xh.UserDto;
import com.lehe.service.impl.AgentInfoService;
import com.lehe.util.LogoConfig;

@Controller
@RequestMapping("xh_wx")
public class XHWeixinWebController extends Basecontroller{
	
	@Value("${config.registUrl}")
	private String registUrl;
	
	@Autowired
    private XHUserListDao xHUserListDao;
	
	@Autowired
    private GoldDetailDao goldDetailDao;
	
	@Autowired
    private AgentInfoService agentInfoService;
	
	@Autowired
	PayRecordDao payRecordDao;
	
	@RequestMapping("xh_goldDetail")
	public String goldDetail(){
		return "user/xh_goldDetail";
	}
	
	@RequestMapping("xh_childList")
	public String childList(@RequestParam(required=false)Long userId,Model m){
		if(userId == null){
			userId = this.getUserId();
		}
		m.addAttribute("userId", userId);
		return "user/xh_childList";
	}
	
	@RequestMapping("xh_childListData")
	@ResponseBody
	public List<UserDto> goldDetailListData(Long userId,Integer page,Integer pagesize){
		List<UserDto> result = xHUserListDao.queryUserList(null,null,null,userId, null, null, null, null, page, pagesize);
		return result;
	}
	
	@RequestMapping("xh_goldDetailList")
	public String goldDetailList(Long agentId,String btime,String etime,Model m){
		m.addAttribute("btime", btime);
		m.addAttribute("etime", etime);
		m.addAttribute("agentId", agentId);
		return "user/xh_goldDetailList";
	}
	
	@RequestMapping("xh_goldDetailListData")
	@ResponseBody
	public List<UserGoldDetailDto> goldDetailListData(String btime,String etime,Integer page,Integer pagesize){
		List<UserGoldDetailDto> result = goldDetailDao.selectGoldDetail(this.getUserId(), btime, etime, page, pagesize);
		return result;
	}
	
	@RequestMapping("registerBarcode")
	public String goldDetailListData(Model m){
		m.addAttribute("userId",this.getUserId());
		return "user/xh_registerBarcode";
	}
	
	@RequestMapping(value = "/registerBarcodeImg", method = { RequestMethod.POST, RequestMethod.GET })
    public void getqcode(HttpServletResponse resp) throws IOException {
		
		String url = String.format(registUrl, this.getUserId());
		
        
        if (url != null && !"".equals(url)) {
            ServletOutputStream stream = null;
            try {

                int width = 200;//图片的宽度
                int height = 200;//高度
                stream = resp.getOutputStream();
                QRCodeWriter writer = new QRCodeWriter();
                
                Map<EncodeHintType,Object> cc = new HashMap<>();
                cc.put(EncodeHintType.CHARACTER_SET,"UTF-8");
                cc.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
                BitMatrix m = writer.encode(url, BarcodeFormat.QR_CODE, height, width,cc);
                
                BufferedImage image = MatrixToImageWriter.toBufferedImage(m, new MatrixToImageConfig(0xFF000001, 0xFFFFFFFF));
                
                LogoConfig logoConfig = new LogoConfig();  
                logoConfig.LogoMatrix(image);  
                
                if (!ImageIO.write(image, "png", stream)) {
                	throw new IOException("Could not write an image of format " + "png");
                }
                
            } catch (WriterException e) {
                e.printStackTrace();
            } finally {
                if (stream != null) {
                    stream.flush();
                    stream.close();
                }
            }
        }
    }
	
	@RequestMapping("xh_applyDetail")
	public String applyDetail(){
		return "user/xh_applyDetail";
	}
	
	@RequestMapping("xh_applyDetailList")
	public String applyDetailList(String btime,String etime,@RequestParam(required=false)String passFlag,Model m){
		m.addAttribute("btime", btime);
		m.addAttribute("etime", etime);
		if(passFlag != null && passFlag.equals("on")){
			m.addAttribute("passFlag", 0);
		}
		return "user/xh_applyDetailList";
	}
	
	@RequestMapping("xh_applyDetailListData")
	@ResponseBody
	public List<ApplyDto> applyDetailListData(String btime,String etime,@RequestParam(required=false)Integer passflag,Integer page,Integer pagesize){
		List<ApplyDto> result = goldDetailDao.selectApplyDetail(this.getUserId(), btime, etime,passflag, page, pagesize);
		return result;
	}
	
	@RequestMapping("xh_applyPass")
	@ResponseBody
	public MessageEnumDto applyPass(Long userId){
		return agentInfoService.passAgentApply(this.getUserId(), userId);
	}
	
}
